package com.power.project.dao.impl;

import com.power.project.dao.Ww_usertagDaoCustom;
import com.power.project.model.Ww_usertag;
import com.power.project.model.dto.UserTagDTO;
import org.hibernate.query.internal.NativeQueryImpl;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.io.Serializable;
import java.util.List;

/**
 * 持久层实现(hibernate)
 */

@Repository
public class Ww_usertagDaoImpl implements Ww_usertagDaoCustom<Ww_usertag, Serializable> {
    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public List<UserTagDTO> getByUserIds(List<String> userIds, String tenantId) {
        StringBuilder dataSql = new StringBuilder("select wt.tagid as \"tagId\", wt.name, wut.wwuserid as \"userId\"\n" +
                "from ww_usertag wut\n" +
                "         left join ww_tag wt on wt.tagid = wut.tagid and wt.tenantid = :tenantid").append(Ww_userDaoImpl.BLANK_STRING);
        StringBuilder whereSql = new StringBuilder("where wut.tenantid = :tenantid\n" +
                "  and wut.wwuserid in (:userIds) and wt.tagid is not null");
        dataSql.append(whereSql);
        Query dataQuery = entityManager.createNativeQuery(dataSql.toString());

        dataQuery.setParameter("userIds", userIds);
        dataQuery.setParameter("tenantid", tenantId);

        return dataQuery.unwrap(NativeQueryImpl.class).setResultTransformer(Transformers.aliasToBean(UserTagDTO.class)).getResultList();

    }

    @Override
    public List<UserTagDTO> getByTagId(String tagId, String tenantId) {
        StringBuilder dataSql = new StringBuilder("select wut.tagid as \"tagId\", wu.userid as \"userId\"\n" +
                "from ww_user wu\n" +
                "         left join ww_usertag wut on wu.userid = wut.wwuserid and wut.tenantid = :tenantId " +
                "left join ha_user hu on wu.userid = hu.userid and hu.deleted = 0").append(Ww_userDaoImpl.BLANK_STRING);
        StringBuilder whereSql = new StringBuilder("where wu.tenantid = :tenantId\n" +
                "  and wut.tagid = :tagId and hu.userid is not null");
        dataSql.append(whereSql);
        Query dataQuery = entityManager.createNativeQuery(dataSql.toString());

        dataQuery.setParameter("tagId", tagId);
        dataQuery.setParameter("tenantId", tenantId);

        return dataQuery.unwrap(NativeQueryImpl.class).setResultTransformer(Transformers.aliasToBean(UserTagDTO.class)).getResultList();
    }

    @Override
    public List<Ww_usertag> queryAllTagUserWechat(String tenantId) {
        String dataSql = "select wut.id,wut.wwuserid,wut.tagid,wut.tenantid\n" +
                "from ww_usertag wut\n" +
                "         left join ww_tag wt on wut.tagid = wt.tagid and wt.tenantid = :tenantId\n" +
                "         left join ww_user wu on wu.userid = wut.wwuserid and wu.tenantid = :tenantId\n" +
                "         left join ha_user hu on wu.userid = hu.userid and hu.deleted = 0\n" +
                "where wut.tenantid = :tenantId and hu.userid is not null and wt.tagid is not null and wu.userid is not null";
        Query dataQuery = entityManager.createNativeQuery(dataSql);

        dataQuery.setParameter("tenantId", tenantId);

        return dataQuery.unwrap(NativeQueryImpl.class).setResultTransformer(Transformers.aliasToBean(Ww_usertag.class)).getResultList();
    }
}